import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'

Vue.use(Vuex)
const modulesFiles = require.context('./modules', true, /\.js$/)

const modules = modulesFiles.keys().reduce((modules, modulePath) => {
    const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
    const value = modulesFiles(modulePath)
    modules[moduleName] = value.default
    return modules
}, {})
export default new Vuex.Store({
    state: {
        // avatar_url: 'https://onestar.newstar.net.cn/images/me.jpg'
        avatar_url: 'https://th.wallhaven.cc/small/zm/zmm7mw.jpg',
        user: JSON.parse(localStorage.getItem('user')) || {}
    },
    mutations: {
        setUserInfo (state, user) {
            console.log('1212', user)
            localStorage.setItem('user', JSON.stringify(user)); // 将传递的数据先保存到localStorage中
            state.user = user; // 之后才是修改state中的状态
        }
    },
    actions: {},
    modules,
    getters
})
